const mongoose = require('mongoose')

const Schema = mongoose.Schema
const Types = Schema.Types

const User = new Schema({
  id: Types.ObjectId,
  username: {
    type: Types.String,
    required: true,
    unique: true,
    index: 1,
    maxlength: 32,
    match: /[a-zA-Z0-9]{5}/ // 用户名只能包含大小写字母以及数字 长度5-20
  },
  password: {
    type: Types.String,
    required: true
  },
  email: {
    type: Types.String,
    required: true,
    unique: true,
    index: 1,
    maxlength: 64,
    match: /[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?/
  },
  createTime: {
    type: Types.Date,
    default: Date.now,
    index: 1
  },
  regCode: {
    type: Types.String,
    required: true,
    unique: true
  },
  avatar: { // 用户头像
    type: Types.Buffer
  },
  state: { // 用户状态
    type: Types.Number,
    default: -1
    /*
     * -1 邮箱未激活
     * 0  正常用户
     */
  }
})

module.exports = mongoose.model('User', User)
